// pages/advert/add_domain/index.js
import config from '../../../config/index.js'
import wxRequest from '../../../utils/wxRequest.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    group_array: [
      {
        id: 1,
        name: 'http'
      },
      {
        id: 2,
        name: 'https'
      }
    ],
    group_index: 0,
    access_type: 'http',
    plan_id: 0, //计划id
    domain: '', //域名
    domain_id: 0, //域名id，编辑域名的时候带过来
    detail: {}, //编辑的详情
  },
  //添加表单提交
  submitFn: function (e) {
    var that = this;
    var formData = e.detail.value; //获取表单所有input的值  
    var map = {
      'domain': formData.domain,
      'plan_id': that.data.plan_id,
      'access_type': that.data.access_type,
      'domain_id': that.data.domain_id
    }
    console.log(map)
    wxRequest.getRequest('/toolkit/domain/save', map).then(res => {
      console.log(res)
      wx.showToast({
        title: res.message,
        icon: res.code == 0 ? 'success' : 'none'
      })
      if (res.code == 0) {
        // that.getList();
        //成功
        wx.navigateBack() //返回上一个页面
      }
    }).catch(error => { })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var plan_id = options.plan_id;
    var domain_id = options.domian_id; //域名id，编辑域名的时候带过来,继续添加域名的时候默认是0
    var title = domain_id == 0 ? '添加域名' : '更新域名';
    that.setData({
      plan_id: plan_id,
      domain_id: domain_id
    })

    wx.setNavigationBarTitle({
      title,  //设置标题
    })

    //判断是否是编辑还是继续添加域名
    if (domain_id > 0) {
      that.getDetail();
    }
  },
  //编辑域名
  getDetail() {
    var map = {
      'id': this.data.domain_id,
    }
    wxRequest.getRequest('/toolkit/domain/edit', map).then(res => {
      console.log(res)
      this.setData({
        detail: res.data,
        group_index: res.data.access_type == 'http' ? 0 : 1,
      })
    }).catch(error => { })
  },
  // 选择类型
  bindPickerChange: function(e) {
    var that = this;
    var access_type = that.data.group_array[e.detail.value].name;
    var group_index = e.detail.value;
      that.setData({
        access_type: access_type,
        group_index: group_index
    })
    console.log(access_type);
  }
  
})